package com.thinkingstar.iads.projectcenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentChildrenType;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquip;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquipInfo;


/**
 * @ClassName: DaoPcProjectEquipmentEquipInfo 
 * @Description:项目中心_项目_设备配备子表
 * @author lyx
 * @date 2018年5月24日
 */
@IocBean(args = {"refer:dao"})
public class DaoPcProjectEquipmentEquipInfo extends EntityRepositoryServiceImpl<PcProjectEquipmentEquipInfo>{

	public DaoPcProjectEquipmentEquipInfo(Dao dao) {
		super(dao);
	}
	
	//根据设备获取设备类型
	public DcEquipmentChildrenType getEquipmentTypeByEquipment(PcProjectEquipmentEquipInfo object){
		String find_sql = new String(" select dect.* "
				+ " from pc_equipment_equip_info peei , dc_equipment de , dc_equipment_children_type dect "
				+ " where peei.equipment_id = de.id and de.equipment_children_type_id = dect.id "
				+ " and de.del_flag = '0' and dect.del_flag = '0' and peei.id = @peei_id ");
		Sql sql = Sqls.create(find_sql);
		sql.params().set("peei_id", object.getId());
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcEquipmentChildrenType.class));
		dao().execute(sql);
		return sql.getObject(DcEquipmentChildrenType.class);
	}
	
	//根据设计配备id查询设备数量信息
	public List<PcProjectEquipmentEquipInfo> findEquipmentEquipInfoByEquipId(String equip_id){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("equipment_equip_id", "=", equip_id);
		return query(criteria);
	}
	
	//根据设备配备id以及设备类型id查询设备数量
	public List <PcProjectEquipmentEquipInfo> findEquipmentEquipInfoByEquipIdAndEquipmentTypeId(String equip_id , String equipment_type_id){
		String find_sql = new String(" select peei.* "
				+ " from pc_equipment_equip_info peei ,  dc_equipment de , dc_equipment_children_type dect "
				+ " where peei.equipment_id = de.id and de.equipment_children_type_id = dect.id "
				+ " and de.del_flag = '0' and dect.del_flag = '0' and dect.id = @dect_id and peei.equipment_equip_id = @equip_id ");
		Sql sql = Sqls.create(find_sql);
		sql.params().set("dect_id", equipment_type_id).set("equip_id", equip_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(PcProjectEquipmentEquipInfo.class));
		dao().execute(sql);
		return sql.getList(PcProjectEquipmentEquipInfo.class);
	}
	
	public QueryResult findListByEquipmentEquipId(NutMap param){
		List <PcProjectEquipmentEquipInfo> list = null;
		Pager pager = null;
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", "0");
		if(param.get("equip_id") != null){
			criteria.where().and("equipment_equip_id", "=",param.get("equip_id"));
		}
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(count(criteria));
			list = query(criteria,pager);
		}else{
			list = query(criteria);
		}
		return new QueryResult(list,pager);
	}
	
	//查询条件
	public QueryResult findResultByEquipId(NutMap param){
		List <NutMap> result = null;
		Pager pager = null;
		StringBuilder find_sql = new StringBuilder("select peei.*, dem.equipment_manufacturer_name , de.equipment_num_for_185 "
				   + " from pc_equipment_equip_info peei "
				   + " left join dc_equipment_manufacturer dem on peei.equipment_manufacturer_id = dem.id"
				   + " and dem.del_flag = '0' "
				   + " left join dc_equipment de on peei.equipment_id = de.id and de.del_flag = '0' "
				   + " where peei.del_flag = '0' ");
		if(param.get("equipment_equip_id") != null){
			find_sql.append(" and peei.equipment_equip_id = @equipment_equip_id ");
		}
		if(param.get("searchName") != null){
			find_sql.append(" and peei.equipment_name like @equipment_name");
		}
		find_sql.append(" order by create_time desc ");
		Sql sql = Sqls.create(find_sql.toString());
		if(param.get("equipment_equip_id") != null){
			sql.params().set("equipment_equip_id", param.get("equipment_equip_id"));
		}
		if(param.get("searchName") != null){
			sql.params().set("equipment_name", "%"+param.get("searchName")+"%");
		}
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findResultNumByEquipId(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new LinkedList<>();
				while (rs.next()) {
					NutMap nutMap = new NutMap();
					nutMap.put("id", rs.getString("id"));
					nutMap.put("equipment_name", rs.getString("equipment_name"));
					nutMap.put("equipment_id", rs.getString("equipment_id"));
					nutMap.put("equipment_num", rs.getInt("equipment_num"));
					nutMap.put("equipment_manufacturer_name", rs.getString("equipment_manufacturer_name"));
					nutMap.put("equipment_manufacturer_id", rs.getString("equipment_manufacturer_id"));
					nutMap.put("equipment_price", rs.getDouble("equipment_price"));
					nutMap.put("equipment_num_for_185", rs.getString("equipment_num_for_185"));
					nutMap.put("comment", rs.getString("comment"));
					nutMap.put("create_time", rs.getString("create_time"));
					nutMap.put("update_time", rs.getString("update_time"));
					list.add(nutMap);
				}
				return list;
			}
		});
		dao().execute(sql);
		result = sql.getList(NutMap.class);
		return new QueryResult(result,pager);
	}
	
	//查询记录数
	public int findResultNumByEquipId(NutMap param){
		StringBuilder find_sql = new StringBuilder("select count(peei.id) num "
				   + " from pc_equipment_equip_info peei "
				   + " left join dc_equipment_manufacturer dem on peei.equipment_manufacturer_id = dem.id "
				   + " and dem.del_flag = '0' "
				   + " where peei.del_flag = '0'");
		if(param.get("equipment_equip_id") != null){
			find_sql.append(" and peei.equipment_equip_id = @equipment_equip_id ");
		}
		if(param.get("searchName") != null){
			find_sql.append(" and peei.equipment_name like @equipment_name");
		}
		Sql sql = Sqls.create(find_sql.toString());
		if(param.get("equipment_equip_id") != null){
			sql.params().set("equipment_equip_id", param.get("equipment_equip_id"));
		}
		if(param.get("searchName") != null){
			sql.params().set("equipment_name", "%"+param.get("searchName")+"%");
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				int i = 0;
				while (rs.next()) {
					i = rs.getInt("num");
				}
				return i;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	

}
